{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.110930Z",
     "start_time": "2021-05-02T22:39:29.105793Z"
    }
   },
   "outputs": [],
   "source": [
    "from platform import python_version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.209157Z",
     "start_time": "2021-05-02T22:39:29.113661Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.6.9'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_version()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.275581Z",
     "start_time": "2021-05-02T22:39:29.214799Z"
    }
   },
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## create functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.342395Z",
     "start_time": "2021-05-02T22:39:29.277167Z"
    }
   },
   "outputs": [],
   "source": [
    "def _tokenize(input_str):\n",
    "    # to split on: ' ', ':', '!', '%', '?', ',', '.', '/'\n",
    "    to_split = r\"(?u)(?:\\s|(\\:)|(!)|(%)|(\\?)|(,)|(\\.)|(\\/))\"\n",
    "        \n",
    "    tokenized_parts = [tok for tok in re.split(to_split, input_str) if tok]\n",
    "        \n",
    "    return \" \".join(tokenized_parts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.420942Z",
     "start_time": "2021-05-02T22:39:29.343948Z"
    }
   },
   "outputs": [],
   "source": [
    "def _remove_duplicated_whitespace(input_str):\n",
    "    return re.sub(r'\\s+', ' ', input_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.487454Z",
     "start_time": "2021-05-02T22:39:29.425058Z"
    }
   },
   "outputs": [],
   "source": [
    "def preprocess(input_str):\n",
    "    \n",
    "    output_str = input_str\n",
    "    \n",
    "    output_str = _tokenize(output_str)\n",
    "    \n",
    "    output_str = _remove_duplicated_whitespace(output_str)\n",
    "    \n",
    "    output_str = output_str.strip(\" \")\n",
    "    \n",
    "    return output_str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.597397Z",
     "start_time": "2021-05-02T22:39:29.490514Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'foo ! bar'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocess(\"foo!  bar\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.680633Z",
     "start_time": "2021-05-02T22:39:29.605267Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'foo ! baz .'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocess(\"foo!baz.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.755131Z",
     "start_time": "2021-05-02T22:39:29.683723Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'foo ! bar , baz .'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocess(\"foo!bar,baz.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We purposefully did not add `-` to the splitter characters so it isn't split on that"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-05-02T22:39:29.844686Z",
     "start_time": "2021-05-02T22:39:29.757282Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'foo-bar'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocess(\"foo-bar\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
